#!/usr/bin/env bash

###############################
# author: wzq
# version: 0.1
# usage:
###############################

_test=0     # 0=no test, 1=test
_debug=0    # 0=debug off, 1=on

function help {
    set +e
    set +x
    printf "%s\n" "$0 options"
    printf "%-8s %s\n" "" " -h : show help"
    printf "%-8s %s\n" "" " -c : compile from code, default option"
    printf "%-8s %s\n" "" " -i : run image, confilict with -c"
    printf "%-8s %s\n" "" " -s : stop"
    printf "%-8s %s\n" "" " -l : show log"
    printf "%-8s %s\n" "" " -t : test"
    printf "%-8s %s\n" "" " -x : show debug info"
    printf "%-8s %s\n" "" " -x : mvn spring-boot:run"
}

function build {
    set -e

    if [ "1" = "$_debug" ]; then
        set -x
    else
        set +x
    fi
    # const
    img_mvn="maven:3.3.3-jdk-8"                 # docker image of maven
    m2_cache="~/.m2"                            # the local maven cache dir
    proj_home="$PWD"                            # the project root dir

    if [ "1" = "$_test" ]; then
        build_cmd="mvn clean test package -U"
    else
        build_cmd="mvn clean package -U -Dmaven.test.skip=true"
    fi

    # variable
    major_module="$proj_home/order-service"     # major service module root folder
    jar_file="order-service-*-SNAPSHOT.jar"     # jar文件名称
    img_name="dormi330/trade"                   # output image tag

    git pull 2> /dev/null

    if which mvn ; then
        echo "use local maven"
        eval $build_cmd
    else
        echo "use docker maven"
        docker run --rm \
            -v $m2_cache:/root/.m2 \
            -v $proj_home:/usr/src/mymaven \
            -w /usr/src/mymaven \
            $img_mvn $build_cmd
    fi

    rc=$?
    if [[ $rc != 0 ]]; then
        ehoc "package fail"
        exit 1
    fi

    mv $major_module/target/$jar_file $major_module/target/service.jar
    docker build -t $img_name .

}

c_name="dormi330_trade"
img_name="dormi330/trade"

function stop_run {
    c_name="dormi330_trade"
    docker rm -f $c_name 2> /dev/null
}

function show_log {
    docker logs -f $c_name
}

function run_image {
    if [ "1" = "$_debug" ]; then
        set -x
    fi
    docker rm -f $c_name 2> /dev/null
    set -e
    # 启动镜像
    docker run -d \
    --restart=on-failure:5 \
    --privileged \
    -w /home \
    -v $PWD/logs:/home/logs \
    -p 19001:19001 \
    -p 9001:9001 \
    --name $c_name \
    $img_name \
    java \
        -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=19001,suspend=n \
        -Djava.security.egd=file:/dev/./urandom \
        -Duser.timezone=Asia/Shanghai \
        -jar /home/service.jar \
            --spring.profiles.active=docker

    docker logs -f $c_name
}

# help
# build
# run_image

cd `dirname $0`

################## script start here ##################
while getopts :hcisltx opt; do
  case "$opt" in
    t) echo "test on";  _test=1 ;;
    x) echo "debug on"; _debug=1 ;;

    h) help ;;
    c) build; run_image ;;
    i) run_image ;;
    s) stop_run ;;
    l) show_log ;;

    *) help ;;
  esac
done

## Bash's getopts will exit the while loop when no options are provided, setting $opt to '?'
# https://unix.stackexchange.com/questions/287190/execute-default-option-when-no-options-are-specified-in-getopts
if (( $OPTIND == 1 )); then
   echo "Default option"
   # build; run_image
   help
fi